package cz.acies.face.modul;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.BeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import cz.acies.data.DBConnector;
import cz.acies.data.persist.ModelObject;
import cz.acies.face.viewers.TableViewer;
import cz.acies.face.viewers.TreeViewer;
import cz.acies.uniql.sql.SelectQuery;
import cz.acies.uniql.sql.element.UniQLCondition;

public class ModulViewer extends ModelObject
{
	private static final long serialVersionUID = 5211769263336719735L;

	protected String modulId; 
	protected String label;
	protected String image;
	protected String pageUrl;
	
	protected TreeViewer treeViewer;
	protected TableViewer tableViewer;  

	public ModulViewer() {
		this(null, null, null);
	}

	public ModulViewer(String id, String label, String image) {
		super(id, null);
		this.label = label;
		this.image = image;
	}

	// -----  -----------------------------------------------------------------
	
	public String getModulId() {
		return modulId;
	}

	public void setModulId(String modulId) {
		this.modulId = modulId;
	}

	public String getLabel() {
		return label;
	}

	public boolean hasLabel() {
		return (label != null && label.length() > 0);
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getImage() {
		return image;
	}

	public boolean hasImage() {
		return (image != null && image.length() > 0);
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getPageUrl() {
		return pageUrl;
	}

	public boolean hasPageUrl() {
		return (pageUrl != null && pageUrl.length() > 0);
	}

	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}
	
	public TreeViewer getTreeViewer() {
		return treeViewer;
	}
	
	public void setTreeViewer(String className) {
		if (className != null && className.length() > 0) {
			try {
				Object obj = Class.forName(className).newInstance();
				this.treeViewer = (TreeViewer) obj;
			} catch (Exception exc) {
				exc.printStackTrace();
			}
		}
	}
	
	public TableViewer getTableViewer() {
		return tableViewer;
	}
	
	public void setTableViewer(String className) {
		if (className != null && className.length() > 0) {
			try {
				Object obj = Class.forName(className).newInstance();
				this.tableViewer = (TableViewer) obj;
			} catch (Exception exc) {
				exc.printStackTrace();
			}
		}
	}
	
	// -----  -----------------------------------------------------------------
	
	public static ModulViewer create(String id, DBConnector db) {
		ModulViewer viewer = null;
		try {
			QueryRunner qr = new QueryRunner(db.getDataSource());
			viewer = qr.query(new SelectQuery(db.dbmd())
				.addFrom("gui_modul")
				.add(new UniQLCondition("modulId", "=", "'"+id+"'"))
				.toSQLString(), 
				new BeanHandler<ModulViewer>(ModulViewer.class, new BasicRowProcessor() {
					@Override
					public <T> T toBean(ResultSet rs, Class<T> type) throws SQLException {
						T obj = super.toBean(rs, type);
						if (obj instanceof ModulViewer) {
							ModulViewer viewer = (ModulViewer) obj;
							
							String className = rs.getString("treeViewer");
							if (className == null || className.length() == 0) {
								className = "cz.acies.face.viewers.TreeViewer";
							}
							viewer.setTreeViewer(className);
							
							className = rs.getString("tableViewer");
							if (className == null || className.length() == 0) {
								className = "cz.acies.face.viewers.TableViewer";
							}
							viewer.setTableViewer(className);
						}
						return obj;
					}					
				})
			);
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return viewer;
	}	
}